// src.controllers.user.services.js

var http = require('../helper/HttpUtil');
var jsonUtil = require('../helper/JsonUtil');

var conf = require('../../config/index');
var log  = require('log4js').getLogger("user.controller");

var userServices = require('../services/index');

exports.initLoad = function(req, res, next) {
    if(!req.session.user){
        res.render('manage/login');
    }else{
        res.render('manage/h5');
    }
};

exports.userPage = function(req,res,next){
    console.log("app.usr local");
    res.locals.message = '';
    var err = req.session.error;
    delete req.session.error;
    if (err) res.locals.message = '<div class="alert alert-error">' + err + '</div>';
    res.render('manage/login');
}

exports.queryByNameAndPwd = function(req, res, callback){
    encryptpassword(req, res);
};

exports.queryById = function (req, res, callback) {
    var resMap={};
    userServices.userLogin(req, res, function(err, result){
        if(err) log.debug(err);
        if(result && result.length > 0){
            var user={
                uid:result[0].u_id,
                username : result[0].username
            };
            resMap.status=1;
            resMap.result=result[0];
            res.send(resMap);
        }else{
            next();
        }
    });
};

exports.userLogout = function(req,res,next){
    // var cookies = req.cookies();
    req.session.user=null;
    req.flash('success', '退出成功');
    var resMap={};
    resMap.status=0;
    resMap.message='退出成功';
    resMap.result=[];
    res.send(resMap);
};

exports.queryAllByPage = function (req, res, callback) {
    userServices.queryAllByPage(req, res, callback);
};

function encryptpassword(req, res){
    var resMap={};
    var username = req.body.username;
    var password = req.body.password;
    if(conf.comm.loginType==1){
        var encryptpwUrl = conf.comm.api.encryptpw + '?type=1&password='+password+'&platformId=1';
        http.get(encryptpwUrl, function(err, data){
            if(!err && data && data.status == '0'){
                userlogin(req, res, username, data.result.encryptpw);
            }else{
                jsonWrite(res, data);
            }
        });
    }else{
        userServices.userLogin(req, res, function(err, data){
            log.debug(data);
            if(err) log.debug(err);
            if(!err && data && data.length > 0){
                var user={
                    uid:resultData[0].u_id,
                    username : data[0].username
                };
                req.session.user = user;
                log.debug(req.session.user);
                resMap.status=1;
                resMap.result=data[0];
                res.send(resMap);
            }else{
                log.error(err);
                jsonUtil.jsonWrite(res, data);
            }
        });
    }
}


function userlogin(req, res, username, resultPassword){
    var url = conf.comm.api.login+'?type=1&userId='+username+'&password='+resultPassword+'&platformId=1';
    http.get(url, function(err, data){
        if(!err && data.status == '0' && data.result.businessCode == '0'){
            var user={
                uid:data.result.user.userid,
                username : data.result.user.username
            };
            req.session.user = data.result.user;
            // log.debug(req.session.user);
            jsonWrite(res, data);
        }else{
            jsonWrite(res, err);
        }

    });
}

function jsonWrite(req, data){
    if (typeof data === 'undefined' || data == null || data.result.businessCode != '0') {
        req.json({
            status: 1,
            message: '操作失败'
        });
    } else if (data.length === 0) {
        req.json({
            status: 2,
            message: '请求成功无数据',
            result:[]
        });
    }
    else {
        req.json({
            status: 0,
            message: '请求成功',
            result: data
        });
    }
}